package com.dant.components.lists
{
    import com.dant.components.lists.smoothinglistclasses.DefaultHighlightSkin;
    import com.dant.components.lists.smoothinglistclasses.SmoothScrollingDefaultItemRenderer;
    import com.dant.interfaces.ISelectable;
    
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.InteractiveObject;
    import flash.events.IEventDispatcher;
    import flash.events.MouseEvent;
    
    import flexlib.containers.ButtonScrollingCanvas;
    
    import mx.collections.ArrayCollection;
    import mx.collections.ICollectionView;
    import mx.collections.IList;
    import mx.collections.ListCollectionView;
    import mx.collections.XMLListCollection;
    import mx.containers.BoxDirection;
    import mx.core.ClassFactory;
    import mx.core.IDataRenderer;
    import mx.core.IFactory;
    import mx.core.IFlexDisplayObject;
    import mx.core.UIComponent;
    import mx.effects.Move;
    import mx.events.CollectionEvent;
    import mx.events.CollectionEventKind;
    import mx.events.FlexEvent;
    import mx.events.ItemClickEvent;
    import mx.styles.CSSStyleDeclaration;
    import mx.styles.ISimpleStyleClient;
    import mx.styles.StyleManager;
    import mx.utils.UIDUtil;


    /*  The styles this component will support.  By declaring them here, 
    *   in metadata, the MXML compiler will allow developers to define inline styles on
    *   MXML tags, and perform property type checking.  Styles should be declared 
    *   using the camelCase notation.
    *   In general, you shouldn't declare styles as inheriting. Inheriting styles 
    *   are applied globally in Flex, so if you declare a style
    *   as inheriting, it may accidentally cascade down to a subcomponent that 
    *   uses the same style name for a different meaning. Use other techniques 
    *   to intentionally propogate style values from components to internal 
    *   sub-components 
    */
    [Style(name="horizontalGap", type="Number", inherit="no")]
    [Style(name="verticalGap", type="Number", inherit="no")]

    /*  A skin is really just another style as far as the Flex compiler is concerned. 
    *   By defining the style as type Class, you can use either the Embed() 
    *   or ClassReference() CSS functions to name either a bitmap or programmatic 
    *   skin via CSS.
    */
    [Style(name="itemHighlightSkin", type="Class", inherit="no")]

    /*  Styles used by the default programmatic highlightSkin 
    */
    [Style(name="highlightRadius", type="Number", inherit="no")]
    [Style(name="highlightColors", type="Array", inherit="no")]

    /*
    *   The events this component will dispatch. By declaring them here, 
    *   in metadata, the MXML compiler will allow developers to attach an event 
    *   handler in MXML.
    *   Otherwise it would see the handler as an attempt to set a non-existant property 
    */  
    [Event(name="itemClick", type="mx.events.ItemClickEvent")] 
    /*  
    *   This component's default property. Declaring a default property allows 
    *   the developer to specify the value of the property as the content of its 
    *   tag in MXML without having to explicitly wrap it in a property tag.  
    *   Be judicious with your use of this feature... only use it in scenarios 
    *   where a developer might reasonably consider the value of this property to be the 'content' 
    *   or intrinsic value of the component.  We consider it best practice not 
    *   to declare scalar values properties (numbers, strings)
    *   as default properties as these are generally best specified as attributes
    */
    [DefaultProperty("dataProvider")]
	public class SmoothScrollingList extends ButtonScrollingCanvas
	{
		
		private static var stylesInitialized:Boolean = setDefaultStyle();

		/**
        * @private
        * Defines a method that sets the default values for the style properties.
        */
        private static function setDefaultStyle():Boolean
        {
            if (!StyleManager.getStyleDeclaration("SmoothScrollingList"))
            {
                // If there is no CSS definition for this class, 
                // then create one and set the default value.
                var defaultStyle:CSSStyleDeclaration = new CSSStyleDeclaration();
                
                var backgroundColor : uint;
                var highlightRadius:int;
                var highlightColors:Array;
                var itemHighlightSkin:Class;
                var verticalGap:int;
                var fontFamily:Array;
                var fontSize:Class;
                var horizontalGap:int;

                defaultStyle.factory = function():void
                {
		            fontFamily: "MyArial";
		            fontSize: 12;
		            verticalGap: 3;
		            horizontalGap: 20;
		            itemHighlightSkin: DefaultHighlightSkin;
		            highlightColors: 0xFFFF99;//, 0xFFDD00];
		            highlightRadius: 4; 
                }
                // Updates the declaration into the CSS
                StyleManager.setStyleDeclaration("SmoothScrollingList", defaultStyle, false);
	            return(true);
            }
            return(false);
        }
		

        /**
        * @private
        *
        */
        private var _dataProvider : ICollectionView;
        private var dataProviderChanged : Boolean;

        /**
        * @private
        *
        */
        private var _direction : String = BoxDirection.VERTICAL;
        private var directionChanged : Boolean = true;

        /**
        * @private
        *
        */
        private var _itemRenderer : IFactory = new ClassFactory(SmoothScrollingDefaultItemRenderer);
        private var itemRendererChanged : Boolean = true;


        /**
         * 
         */        
        protected var highlight:IFlexDisplayObject;
        private var highlightChanged:Boolean = false;
        
        /**
         * 
         */        
        private var _highlightedItem:Object;
        private var highlightedItemChanged:Boolean;

        private var horizontalGapChanged:Boolean;

        /**
         * 
         */     
        private var _labelField:String;
        private var labelFieldChanged:Boolean;
        
        private var layoutPending:Boolean;


        protected var dataToRendererMap:Object = {};
		/**
		 * 
		 */		
		protected var rendererToDataMap:Object = {};
		protected var renderers:Array;

        private var updatedItems:int;

		/**
		 * 
		 */		
		private var verticalGapChanged:Boolean;

        
        [Bindable]
	    /**
	     *  @private
	     */
	    public function set dataProvider(value:Object):void
	    {
	        if (_dataProvider)
	        {
	            _dataProvider.removeEventListener(CollectionEvent.COLLECTION_CHANGE, 
	                                                       dataProvider_changeHandler);
	        }
	
	        if (value is Array)
	        {
	            _dataProvider = new ArrayCollection(value as Array);
	        }
	        else if (value is ICollectionView)
	        {
	            _dataProvider = ICollectionView(value);
	        }
	        else if (value is IList)
	        {
	            _dataProvider = new ListCollectionView(IList(value));
	        }
	        else if (value is XMLList)
	        {
	            _dataProvider = new XMLListCollection(value as XMLList);
	        }
	        else if (value is XML)
	        {
	            var xl:XMLList = XML(value).children();
	            _dataProvider = new XMLListCollection(xl);
	        }
	        else
	        {
	            // convert it to an array containing this one item
	            var tmp:Array = [];
	            if (value != null)
	                tmp.push(value);
	            _dataProvider = new ArrayCollection(tmp);
	        }

	        // trace("ListBase added change listener");
	        _dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, 
	                                   dataProvider_changeHandler, false, 0, true);
	
	        dataProviderChanged = true;

	        var event:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
	        event.kind = CollectionEventKind.RESET;
	        dispatchEvent(event);

	        invalidateProperties();
	    }

        public function get dataProvider():Object
        {
            return _dataProvider;
        }

		[Inspectable(category="Layout", defaultValue="vertical", 
			enumeration="vertical,horizontal",
			listOffset=0, name="direction", type="String")]

        [Bindable]
        /**
        *
        *
        */
        public function set direction(value:String):void
        {
            if ( _direction != value )
            {
                _direction = value;
                directionChanged = true;
                invalidateProperties();
            }
        }
        
        public function get direction():String
        {
            return _direction;
        }

        [Bindable]
        /**
        *
        *
        */
        protected function set highlightedItem(value:Object):void
        {
            if ( _highlightedItem != value )
            {
                _highlightedItem = value;
                highlightedItemChanged = true;
                invalidateDisplayList();
            }
        }
        
        protected function get highlightedItem():Object
        {
            return _highlightedItem;
        }

        [Bindable]
        /**
        *
        *
        */
        public function set itemRenderer(value:IFactory):void
        {
            if ( _itemRenderer != value )
            {
                _itemRenderer = value;
                itemRendererChanged = true;
                invalidateProperties();
            }
        }
        
        public function get itemRenderer():IFactory
        {
            return _itemRenderer;
        }


        /**
        *
        *
        */
        public function set labelField(value:String):void
        {
            if ( _labelField != value )
            {
                _labelField = value;
                labelFieldChanged = true;
                invalidateProperties();
            }
        }

        public function get labelField():String
        {
            return _labelField;
        }

		public function SmoothScrollingList()
		{
			super();
		}

        override protected function createChildren():void
        {
            super.createChildren();
            
            createHighlight();
            var highlightClass:Class = getStyle("itemHighlightSkin");
            if (highlightClass != null)
            {
                highlight = new highlightClass();
                if(highlight is ISimpleStyleClient)
                {
                    ISimpleStyleClient(highlight).styleName = this;
                }
                rawChildren.addChild(DisplayObject(highlight));
            }

        }
        
        override protected function childrenCreated():void
        {
        	super.childrenCreated();
        }


        override protected function commitProperties():void
        {
            super.commitProperties();
            if ( directionChanged )
            {
            	layout(direction);
            	directionChanged = false;
            }
            if ( dataProviderChanged || itemRendererChanged || labelFieldChanged )
            {
                if (!renderers)
                {
                	renderers = [];
                }
                
                if ( verticalScrollPosition != 0 )
                {
                	verticalScrollPosition = 0;
                }
                
                if ( horizontalScrollPosition != 0 )
                {
                	horizontalScrollPosition = 0;
                }

                updatedItems = 0;

                var renderersLength:int = renderers.length;

                var n:int = _dataProvider ? _dataProvider.length : 0;
                
                if ((n==0) && highlight)
                {
                	validateHighlightVisibility();
                }
                
                var itemX:int = 0;
                var itemY:int = 0;
                
                for (var i:int=0; i<n; i++ )
                {
                    var dataValue:Object = _dataProvider[i];
                    var itemData:Object = parseDataForRender(dataValue);

                    var item:UIComponent;

                    if (i >= renderersLength)
                    {
                        item = addRenderer(itemData);
                    }
                    else
                    {
                    	item = renderers[i];
                    	if ( item is IDataRenderer )
                    	{
                    		assignDataToRenderer(IDataRenderer(item),itemData);
                    	}
                    	if ( item is ISelectable )
                    	{
                    		ISelectable(item).unselect();
                    	}
                    }
                    item.visible = false;
                    item.addEventListener(FlexEvent.UPDATE_COMPLETE, 
                                            item_updateHandler, false, 0, true);
                    item.setStyle("moveEffect",new Move(item));
                    if ( direction == BoxDirection.VERTICAL )
                    {
	                    itemX = Math.max(((width/2) - (item.width/2)),0);
	                    itemY = itemY+8;
                    }
                    else
                    {
	                    itemX = itemX+8;
	                    itemY = Math.max(((height/2) - (item.height/2)),0);
                    }                    
                    item.x = itemX;
                    item.y = itemY;
                    associateDataAndRenderer(item,dataValue);
                }
                
                if (i<renderersLength)
                {
                	var m:int = renderersLength;
                	for (var j:int = renderersLength-1; j>=i; j--)
                	{
                		if (renderers[i])
                		{
	                		var child:DisplayObject = DisplayObject(renderers[i]);
	                		var children:Array = getChildren();
	                		if (contains(child))
	                		{
	                			// removes the child and refreshes the renderers array!
	                			removeChild(child);
	                		}
                		}
                	}
                }

                dataProviderChanged = false;
                itemRendererChanged = false;
                labelFieldChanged = false;
            }
        }

        override protected function measure():void
        {
        	super.measure();

        	var children:Array = getChildren();
        	var n:int = children ? children.length:0;
        	
        	var i:int;
        	var calculatedWidth:int = 0;
        	var calculatedHeight:int = 0;
        	var verticalGap:int = getStyle("verticalGap");
        	var horizontalGap:int = getStyle("horizontalGap");
        	var item:DisplayObject;

        	
        	if ( direction == BoxDirection.VERTICAL )
        	{
	        	if (isNaN(verticalGap))
	        	{
	        		verticalGap = 0;
	        	}
	        	for (i=0;i<n;i++)
	        	{
	        		item = DisplayObject(children[i]);
	        		calculatedWidth = Math.max(item.width,calculatedWidth);
	        		calculatedHeight = calculatedHeight + item.height + verticalGap;
	        	}
	        	
        	}
        	else if ( direction == BoxDirection.HORIZONTAL )
        	{
	        	if (isNaN(horizontalGap))
	        	{
	        		horizontalGap = 0;
	        	}
	        	for (i=0;i<n;i++)
	        	{
	        		item = DisplayObject(children[i]);
	        		calculatedWidth = calculatedWidth + item.width + horizontalGap;
	        		calculatedHeight = Math.max(item.height,calculatedHeight);
	        	}
        	}
        	measuredHeight = calculatedHeight;
        	measuredWidth = calculatedWidth;
        	
        	
        	if (layoutPending)
        	{
        		layout(direction);
        		layoutPending = false;
        	}
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
        	super.updateDisplayList(unscaledWidth,unscaledHeight);
            if (highlightChanged)
            {
            	if (highlight)
            	{
            		rawChildren.removeChild(DisplayObject(highlight));
	            	highlight = null;
            	}
	            var highlightClass:Class = getStyle("itemHighlightSkin");
	            if (highlightClass != null)
	            {
	                highlight = new highlightClass();
	                if(highlight is ISimpleStyleClient)
	                {
	                    ISimpleStyleClient(highlight).styleName = this;
	                }
	                rawChildren.addChild(DisplayObject(highlight));
//	                highlight.parent.setChildIndex(highlight.parent,0);
	            }
            	highlightChanged = false;
            }

            if (horizontalGapChanged)
            {
            	if ( direction == BoxDirection.HORIZONTAL )
            	{
	            	var horizontalGap:int = getStyle("horizontalGap");
	            	if (isNaN(horizontalGap))
	            	{
	            		horizontalGap = 0;
	            	}
	            	
	            	layoutHorizontal(horizontalGap);
            	}
            	// horizontal gap functionality here
            	horizontalGapChanged = false;
            }
            
            if (verticalGapChanged)
            {
            	if ( direction == BoxDirection.VERTICAL )
            	{
	            	var verticalGap:int = getStyle("verticalGap");
    	        	if (isNaN(verticalGap))
        	    	{
            			verticalGap = 0;
            		}
            		layoutVertical(verticalGap);
            	}
            	// vertical gap functionality here
            	verticalGapChanged = false;
            }
            
            if (highlightedItemChanged)
            {
            	if (highlight)
            	{
	            	if (!highlightedItem)
	            	{
	            		highlight.visible = false;
	            	}
	            	else
	            	{
	            		var displayItem:DisplayObject = DisplayObject(highlightedItem);
	            		var highlightedItemParent:DisplayObjectContainer = displayItem.parent;
	            		highlightedItemParent.setChildIndex(displayItem,
	            		                 highlightedItemParent.numChildren-1);
	            		var children:Array = getChildren();
	            		highlight.visible = true;
	            		if ( direction == BoxDirection.VERTICAL )
	            		{
		            		highlight.x = displayItem.x;
		            		highlight.y = (verticalScrollPosition>0)?
		            		         (displayItem.y-verticalScrollPosition):displayItem.y;
	            		}
	            		else if ( direction == BoxDirection.HORIZONTAL )
	            		{
		            		highlight.x = (horizontalScrollPosition>0)?
		            				 (displayItem.x-horizontalScrollPosition):displayItem.x;
		            		highlight.y = displayItem.y;
	            		}
	            		highlight.setActualSize(displayItem.width,displayItem.height);
	            	}
            	}
            	highlightedItemChanged = false;
            }
        }

        override public function styleChanged(styleProp:String):void
        {
        	super.styleChanged(styleProp);
        	if ( styleProp == "horizontalGap" )
        	{
        		horizontalGapChanged = true;
        		invalidateDisplayList();
        	}
        	if ( styleProp == "verticalGap" )
        	{
        		verticalGapChanged = true;
        		invalidateDisplayList();
        	}
        	if ( styleProp == "itemHighlightSkin" )
        	{
        		highlightChanged = true;
        		invalidateDisplayList();
        	}
        	if ( styleProp == "highlightRadius" )
        	{
                highlightChanged = true;
                invalidateDisplayList();
        	}
        	if ( styleProp == "highlightColors" )
        	{
                highlightChanged = true;
                invalidateDisplayList();
        	}
        }

        override public function removeAllChildren():void
        {
            super.removeAllChildren();
            var n:int = renderers ? renderers.length : 0;
            for (var i:int = 0; i<n; i++)
            {
            	var child:DisplayObject = DisplayObject(renderers[i]);
            	removeListenersFromItem(child);
            }
            renderers = null;
        }

        override public function removeChild(child:DisplayObject):DisplayObject
        {
        	var index:int = renderers.indexOf(child);
        	if (index >= 0)
        	{
        		removeListenersFromItem(child);
        		deassociateDataAndRenderer(child,IDataRenderer(child).data);
        		// removes the instances not being used by the new dataProvider
        		renderers.splice(index,1);
        	}
            return (super.removeChild(child));
        }
        
        override public function removeChildAt(index:int):DisplayObject
        {
        	var child:DisplayObject = super.removeChildAt(index);
        	if (child)
        	{
	        	removeListenersFromItem(child);
	        	deassociateDataAndRenderer(child,IDataRenderer(child).data);
	        	// removes the instances not being used by the new dataProvider
	        	renderers.splice(index,1);
        	}
        	return(child);
        }
        
        protected function addListenersToItem(item:IEventDispatcher):void
        {
        	item.addEventListener(MouseEvent.CLICK,item_clickHandler, false, 0, true);
        	item.addEventListener(MouseEvent.ROLL_OVER, item_rollOverHandler, false, 0, true);
        	item.addEventListener(MouseEvent.ROLL_OUT,item_rollOutHandler, false, 0, true);
        }

        protected function removeListenersFromItem(item:IEventDispatcher):void
        {
        	item.removeEventListener(MouseEvent.CLICK,item_clickHandler);
        	item.removeEventListener(MouseEvent.ROLL_OVER, item_rollOverHandler);
        	item.removeEventListener(MouseEvent.ROLL_OUT,item_rollOutHandler);
        }

        protected function addRenderer(data:Object):UIComponent
        {
            var item:UIComponent = createItem(itemRenderer,data);
            addChild(item);
            if (!renderers)
            {
            	renderers = [];
            }
            renderers.push(item);
            return(item);
        }

        protected function associateDataAndRenderer(itemRenderer:Object,data:Object):void
        {
        	rendererToDataMap[UIDUtil.getUID(itemRenderer)] = data;
        	dataToRendererMap[UIDUtil.getUID(data)] = itemRenderer;
        }

        protected function deassociateDataAndRenderer(itemRenderer:Object,data:Object):void
        {
        	delete(rendererToDataMap[UIDUtil.getUID(itemRenderer)]);
        	delete(dataToRendererMap[UIDUtil.getUID(data)]);
        }

        protected function assignDataToRenderer(item:IDataRenderer,data:Object):void
        {
        	item.data = data;
        }

        protected function createItem(itemRenderer:IFactory,data:Object):UIComponent
        {
            var item:UIComponent = UIComponent(itemRenderer.newInstance());
            addListenersToItem(item);
            if (item is IDataRenderer)
            {
                assignDataToRenderer(IDataRenderer(item),data);
            }
            return(item);
        }

        protected function getDataFromRenderer(item:DisplayObject):Object
        {
        	return (rendererToDataMap[UIDUtil.getUID(item)]);
        }

        protected function getRendererFromData(data:Object):DisplayObject
        {
        	return (dataToRendererMap[UIDUtil.getUID(data)]);
        }

        protected function layout(direction:String):void
        {
            if (direction == BoxDirection.VERTICAL)
            {
	            var verticalGap:int = getStyle("verticalGap");
	            if (isNaN(verticalGap))
	            {
	                verticalGap = 0;
	            }
	            layoutVertical(verticalGap);
            }
            else if (direction == BoxDirection.HORIZONTAL)
            {
                var horizontalGap:int = getStyle("horizontalGap");
                if (isNaN(horizontalGap))
                {
                    horizontalGap = 0;
                }
                layoutHorizontal(horizontalGap);
            }
        	invalidateSize();
        }

        protected function layoutHorizontal(horizontalGap:int):void
        {
        	var itemX:int = 0;
        	var n:int = _dataProvider ? _dataProvider.length : 0;
            for (var i:int=0; i<n; i++ )
            {
                var data:Object = _dataProvider[i];
                var item:DisplayObject = getRendererFromData(data);
                if (item)
                {
                    item.x = itemX;
                    item.y = Math.max(((height/2) - (item.height/2)),0);
                    item.visible = true;
                    itemX = itemX + item.width + horizontalGap; 
                }
            }
        }

        protected function layoutVertical(verticalGap:int):void
        {
            var itemY:int = 0;
            var n:int = _dataProvider ? _dataProvider.length : 0;
            for (var i:int=0; i<n; i++ )
            {
                var data:Object = _dataProvider[i];
                var item:DisplayObject = getRendererFromData(data);
                if (item)
                {
                    item.x = Math.max(((width/2) - (item.width/2)),0);
                    item.y = itemY;
                    item.visible = true;
                    itemY = itemY + item.height + verticalGap; 
                }
            }
        }

        protected function parseDataForRender(unparsedData:Object):Object
        {
            if (labelField && labelField.length>0)
            {
                if (unparsedData is XML)
                {
                    return(unparsedData[labelField] ? unparsedData[labelField].toString():"");
                }
                else
                {
                    return(unparsedData[labelField]);
                }
            }
            else
            {
                return(unparsedData);
            }
        }

        protected function createHighlight():void
        {
            var highlightClass:Class = getStyle("itemHighlightSkin");

            highlight = highlightClass ? new highlightClass() : new DefaultHighlightSkin();
            if(highlight is ISimpleStyleClient)
            {
                ISimpleStyleClient(highlight).styleName = this;
            }
            rawChildren.addChild(DisplayObject(highlight));
        }

        protected function validateHighlightVisibility():void
        {
        	if (_dataProvider && _dataProvider.length>0)
        	{
        		return;
        	}
        	
        	// if there are no items in the dataProvider
        	if (highlight)
        	{
        		highlight.visible = false;
        	}
        }

        protected function item_clickHandler(event:MouseEvent):void
        {
        	var child:DisplayObject = DisplayObject(event.currentTarget);
        	var item:Object = getDataFromRenderer(child);
        	
        	var index:int = -1;
        	var n:int = _dataProvider.length;
        	for (var i:int=0; i<n; i++)
        	{
        		if ( _dataProvider[i] == item )
        		{
        			index = i;
        			break;
        		}
        	}

        	var itemClickEvent:ItemClickEvent = new ItemClickEvent(ItemClickEvent.ITEM_CLICK,
        	                                    false,true,
        	                                    item.hasOwnProperty(labelField)?item[labelField]:null,
        	                                    index,
        	                                    (child is InteractiveObject)?
        	                                    InteractiveObject(child):null,
        	                                    item);
            dispatchEvent(itemClickEvent);

            if (child is ISelectable)
            {
            	ISelectable(child).select();
            }
            if (highlight)
            {
                highlight.visible = false;
            }
        }

        protected function item_rollOverHandler(event:MouseEvent):void
        {
        	var item:DisplayObject = DisplayObject(event.currentTarget);
        	highlightedItem = item;
        	invalidateDisplayList();
        }

        protected function item_rollOutHandler(event:MouseEvent):void
        {
        	highlightedItem = null;
        	invalidateDisplayList();
        }
        
        protected function item_updateHandler(event:FlexEvent):void
        {
        	var item:IEventDispatcher = IEventDispatcher(event.currentTarget);
        	updatedItems++;
        	if (updatedItems == _dataProvider.length)
        	{
        		layoutPending = true;
        		invalidateSize();
        	}
            item.removeEventListener(FlexEvent.UPDATE_COMPLETE,item_updateHandler);
        }

        protected function dataProvider_changeHandler (event:CollectionEvent):void
        {
        	var n:int = 0;
        	var i:int = 0;
        	var item:UIComponent;
        	var data:Object;
        	switch (event.kind)
        	{
        		case CollectionEventKind.ADD:
        		{
                    n = event.items ? event.items.length:0;
                    for (i=0;i<n;i++)
                    {
//                    	data = event.items[i];
//	                    item = addRenderer(parseDataForRender(data));
//	                    item.addEventListener(FlexEvent.UPDATE_COMPLETE, 
//	                                       item_updateHandler, false, 0, true);
//	                    associateDataAndRenderer(item,data);
                        dataProviderChanged = true;
                        invalidateProperties();
                    }
        			break;
        		}
        		case CollectionEventKind.REFRESH:
        		{
                    dataProviderChanged = true;
                    invalidateProperties();
        			break;
        		}
        		case CollectionEventKind.REMOVE:
        		{
//        			n = event.items ? event.items.length:0;
//        			for (i=0;i<n;i++)
//        			{
//        				data = event.items[i];
//        				item = UIComponent(getRendererFromData(data));
//        				removeChild(item);
//        				updatedItems--;
//        			}
                    dataProviderChanged = true;
                    invalidateProperties();
        			invalidateSize();
        			validateHighlightVisibility();
        			break;
        		}
        		case CollectionEventKind.REPLACE:
        		{
                    dataProviderChanged = true;
                    invalidateProperties();
        			break;
        		}
        		case CollectionEventKind.UPDATE:
        		{
                    dataProviderChanged = true;
                    invalidateProperties();
        			break;
        		}
        	}
        }
	}
}